package sbpme.designer.command;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

import sbpme.designer.model.Activity;
import sbpme.designer.model.ProcessDiagram;

public class ActivityCreateCommand extends Command {
	
	private Activity activity;
	private Point location;
	protected ProcessDiagram parent;
	
	public boolean canExecute() {
//		return node.isPossibleChildOf(parent);
		return true;
	}
	
	private void setActivityName() {
		if (activity.getName() == null) {
			activity.setName("activity."+((int)1000*Math.random()));
		}
	}

	public void execute() {
		setActivityConstraint();
		setActivityName();
		parent.addActivity(activity);
	}
	
	private void setActivityConstraint() {
		if (location != null) {
			activity.setConstraint(new Rectangle(location, new Dimension(100, 50)));
		}		
	}
	
	public void undo() {
		parent.removeActivity(activity);
	}
	
	public void setActivity(Activity activity) {
		this.activity = activity;
	}
	
	public void setLocation(Point location) {
		this.location = location;
	}
	
	public void setParent(ProcessDiagram parent) {
		this.parent = parent;
	}
}